package com.gzbugu.polytechsasia.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gzbugu.polytechsasia.bean.PageModel;
import com.gzbugu.polytechsasia.dao.DatabaseLinkDAO;
import com.gzbugu.polytechsasia.factory.DAOFactory;
import com.gzbugu.polytechsasia.service.IShowArticleService;
import com.gzbugu.polytechsasia.vo.Article;

/**
 * 获得文章service
 * 
 * @author gauze
 * 
 */
public class ShowArticleService implements IShowArticleService {

	private static Logger log = LoggerFactory
			.getLogger(ShowArticleService.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.polytechsasia.service.impl.IShowArticleService#findAllTitleJSON
	 * (int, int, int)
	 */
	public String findAllTitleJSON(int type, int currentPage, int pageSize) {
		log.debug("find all title by type " + type);
		JSONArray titleJsonArray = new JSONArray();
		JSONObject alltitleJsonObject = new JSONObject();
		String prepareSql = "SELECT id,title FROM Article WHERE type = ? ORDER BY id LIMIT ?,?";
		Connection connection = DatabaseLinkDAO.getConnection();
		try {
			PreparedStatement statement = connection
					.prepareStatement(prepareSql);
			statement.setInt(1, type);// 注入参数
			statement.setInt(2, (currentPage - 1) * pageSize);
			statement.setInt(3, pageSize);
			ResultSet resultSet = statement.executeQuery();// 查询
			while (resultSet.next()) {
				JSONObject titleJsonObject = new JSONObject();
				titleJsonObject.accumulate("id", resultSet.getInt(1));
				titleJsonObject.accumulate("title", resultSet.getString(2));
				titleJsonArray.put(titleJsonObject);
			}
			DatabaseLinkDAO.free(resultSet, statement, connection);// 施放资源
			alltitleJsonObject.accumulate("titles", titleJsonArray);
			alltitleJsonObject.accumulate("page", findPageJSON(type,
					currentPage, pageSize));
			log.debug("find all title successful, data: "
					+ alltitleJsonObject.toString());
			return alltitleJsonObject.toString();
		} catch (SQLException e) {
			log.error("find all title failed and return json with nothing", e);
			DatabaseLinkDAO.free(null, null, connection);
			return new JSONArray().toString();
		} catch (JSONException e) {
			log.error("find all title failed and return json with nothing", e);
			DatabaseLinkDAO.free(null, null, connection);
			return new JSONArray().toString();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.polytechsasia.service.impl.IShowArticleService#findArticleJSONById
	 * (int)
	 */
	public String findArticleJSONById(int id) {
		log.debug("find article by id: " + id);
		JSONObject articleJsonObject = new JSONObject();
		try {
			Article article = DAOFactory.getArticleDAO().findById(id);// 查询
			if (article != null) {
				articleJsonObject.accumulate("id", article.getId());
				articleJsonObject.accumulate("title", article.getTitle());
				articleJsonObject.accumulate("content", article.getContent());
			}
			log.debug("find article successful, data: "
					+ articleJsonObject.toString());
			return articleJsonObject.toString();
		} catch (SQLException e) {
			log.error("find article failed and return json with nothing", e);
			return new JSONObject().toString();
		} catch (JSONException e) {
			log.error("find article failed and return json with nothing", e);
			return new JSONObject().toString();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.polytechsasia.service.IShowArticleService#findPageJSON(int,
	 * int, int)
	 */
	public JSONObject findPageJSON(int type, int currentPage, int pageSize) {
		log.debug("find pageModel by type " + type);
		JSONObject pageJson = new JSONObject();
		String prepareSql = "SELECT COUNT(*) FROM Article WHERE type = ?";
		Connection connection = DatabaseLinkDAO.getConnection();
		try {
			PageModel pageModel = new PageModel();
			pageModel.setCurrentPage(currentPage);// 当前页
			pageModel.setPageSize(pageSize);// 每页记录数
			PreparedStatement statement = connection
					.prepareStatement(prepareSql);
			statement.setInt(1, type);// 注入参数
			ResultSet resultSet = statement.executeQuery();// 查询
			if (resultSet.next()) {
				int count = resultSet.getInt(1);
				pageModel.setLineCount(count);// 记录总数
				DatabaseLinkDAO.free(resultSet, statement, connection);// 施放资源
			}
			pageJson.accumulate("currentPage", pageModel.getCurrentPage());
			pageJson.accumulate("pageSize", pageModel.getPageSize());
			pageJson.accumulate("totalPages", pageModel.getTotalPages());
			pageJson.accumulate("lineCount", pageModel.getLineCount());
			log
					.debug("find pageModel successful, data: "
							+ pageJson.toString());
			return pageJson;
		} catch (SQLException e) {
			log.error("find pageModel failed", e);
			return new JSONObject();
		} catch (JSONException e) {
			log.error("find pageModel failed", e);
			return new JSONObject();
		}
	}
}
